//
//  InformDetailsCell1.m
//  Village
//
//  Created by 程虎 on 2017/10/10.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "InformDetailsCell1.h"

@implementation InformDetailsCell1

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"InformDetailsCell1";
    InformDetailsCell1 *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    return cell;
}
- (void)setModel:(AnnounModel *)model
{
    _model = model;
    NSString *str = _model.content;
    str = [self htmlEntityDecode:str];
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
   [NSNumber numberWithInt:NSUTF8StringEncoding];
    

    
    NSString *times = _model.createtime;
    if (times.length > 16) {
        times = [times substringWithRange:NSMakeRange(0, 10)];
    }
    self.timeLab.text = [NSString stringWithFormat:@"%@·%@",_model.creator,times];
    self.titleLab.text = _model.title;
    

//    self.contentLab.backgroundColor = [UIColor redColor];
    self.contentLab.numberOfLines = 0;
    [self.contentLab sizeToFit];
    self.contentLab.font = [UIFont systemFontOfSize:14];
    self.contentLab.attributedText = attrStr;
    if ([_model.isread isEqualToString:@"1"]) {
        self.titleLab.textColor = [Util colorWithHexString:@"#999999"];
        self.contentLab.textColor = [Util colorWithHexString:@"#999999"];
    }
    else {
        self.titleLab.textColor = [Util colorWithHexString:@"#333333"];
        self.contentLab.textColor = [Util colorWithHexString:@"#666666"];
    }
    CGFloat height1;
//    height1 = [self getHeightByWidth:_contentLab.frame.size.width title:attrStr font:[UIFont systemFontOfSize:14]] - 3000 ;
    //自定义适应富文本高度
    height1 = [self.contentLab sizeThatFits:CGSizeMake(kDeviceWidth -30,CGFLOAT_MAX)].height;
    
//    height1 = [self hideLabelLayoutHeight:str withTextFontSize:14] -200;
    self.contentLab.sd_layout
    .heightIs(height1);
}
- (CGFloat)getHeightByWidth:(CGFloat)width title:(NSAttributedString *)title font:(UIFont *)font
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.attributedText = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}
- (NSString *)htmlEntityDecode:(NSString *)string
{
    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"]; // Do this last so that, e.g. @"&amp;lt;" goes to @"&lt;" not @"<"
    // 图片自适应
    
    //    string = [NSString stringWithFormat:@"<html> \n"
    //              "<head> \n"
    //              "<style type=\"text/css\"> \n"
    //              "</style> \n"
    //              "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">"
    //              "</head> \n"
    //              "<body>"
    //              "<script type='text/javascript'>"
    //              "window.onload = function(){\n"
    //              "var $img = document.getElementsByTagName('img');\n"
    //              "for(var p in  $img){\n"
    //              "}\n"
    //              "}"
    //              "</script>%@"
    //              "$img[p].style.width = '100%%';\n"
    //              "$img[p].style.height ='100%%';\n"
    //              "</body>"
    //              "</html>",string];
    
    string = [NSString stringWithFormat:@"<head><style>img{max-width:340px !important;}</style></head>%@",string];
    return string;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    CGFloat left = 15;
    CGFloat top = 15.5;
    
    self.typeLab.sd_layout
    .leftSpaceToView(self.contentView, left)
    .topSpaceToView(self.contentView, top)
    .widthIs(60)
    .heightIs(18);
    
    self.timeLab.sd_layout
    .leftSpaceToView(self.typeLab, 9)
    .centerYEqualToView(self.typeLab)
    .rightSpaceToView(self.contentView, left)
    .heightIs(11.5);
    
    self.titleLab.sd_layout
    .leftEqualToView(self.typeLab)
    .topSpaceToView(self.timeLab, 17.5)
    .rightEqualToView(self.timeLab)
    .autoHeightRatio(0);
    
    self.contentLab.sd_layout
    .leftEqualToView(self.titleLab)
    .topSpaceToView(self.titleLab, 14.5)
    .rightEqualToView(self.titleLab)
    .heightIs(0);
    
    self.line.sd_layout
    .leftSpaceToView(self.contentView, 10)
    .topSpaceToView(self.contentLab, 19.5)
    .rightSpaceToView(self.contentView, 10)
    .heightIs(0.5);
    
    [self setupAutoHeightWithBottomView:self.line bottomMargin:0];
}

@end
